<?php

class CompanyMessages extends BaseEntity{

	public static $type = "CompanyMessages";

	function __construct($id = 0){
		parent::__construct($id);
	}
	
	/*
	 * Retorna los mensajes que tienen remanente
	 */
	function getActiveMessages(User $company){
		return $this->search("`companyId` = ".$company->id." AND `amount` > `count`");
	}
	
	function getBroadcastMessages(User $user, $limit = null){
		$companyMessagesSent = new CompanyMessagesSent();
		$sendMessages = $companyMessagesSent->getSentMessages($user, $limit);
		
		$messages = Array();
		foreach ($sendMessages as $sendMessage){
			array_push($messages, CompanyMessages::getById($sendMessage->companyMessageId));
		}
		
		return $messages;
	}
	
	function getUnnotifiedBroadcastMessages(User $loggedUser, $distance = 1000){
		$user = new User();
		$companies = $user->getCompanies();
		
		$nearCompanies  = array();
		foreach ($companies as $company){
			if($company->id != $loggedUser->id){
				$meters = round(distance($loggedUser->latitud, $loggedUser->longitud, $company->latitud, $company->longitud)* 1000);

				if($meters < $distance){
					/*
					 * El usuario entro en el radio de distancia de la empresa
					 */
					 
					 $userProfile = new PersonProfileInfo($loggedUser->profileInfoId);
					 $companyProfile = new CompanyProfileInfo($company->profileInfoId);
					 
					 $companyTags = explode(",", $companyProfile->tags);
					 $userTags = explode(",", $userProfile->interestTags);

					 if (count($userTags) == 0 || count(array_intersect($companyTags, $userTags)) > 0){						
						//echo $loggedUser->username.": ".$loggedUser->latitud." | ".$loggedUser->longitud."<br/>";
						//echo $company->username.": ".$company->latitud." | ".$company->longitud."<br/>";
						//echo "distance to ".$company->username." = $meters mts lower than $distance<br/>";

						array_push($nearCompanies, $company);
					 }
				}
			}
		}
		
		$companyMessageSent = new CompanyMessagesSent();
		$nearMessages = Array();
		foreach ($nearCompanies as $company){
			$messages = $this->getActiveMessages($company);

			foreach ($messages as $message){
				if (!$companyMessageSent->wasSent($message, $loggedUser)){
					array_push($nearMessages, $message);
					
					$message->incrementCounter();
					CompanyMessagesSent::markAsSent($message, $loggedUser);
				}
			}			
		}

		return $nearMessages;
	}
	
	function incrementCounter(){
		//aumento el contador del mensaje
		$this->count += 1;
		$this->save(false);
	}
	
	static function getById($id){
		$cm = new CompanyMessages();
		$cm->getByPattern("`id` = $id");
		return $cm;
	}
	
	static function buildMessagesResponseFromArray($messages){
		$response = Array();
		foreach ($messages as $attr){
			$msg = new stdClass();
			$msg->title = $attr["title"];
			$msg->text = $attr["text"];
			$msg->amount = $attr["amount"];
			$msg->count = $attr["count"];
			
			$response[] = $msg;
		}
		
		return $response;
	}
	
	static function buildMessagesResponseFromObj($messages, $includeCompanyInfo = true){
		$response = Array();
		foreach ($messages as $attr){
			$msg = new stdClass();
			
			if ($includeCompanyInfo){
				$company = new User($attr->companyId);
				$profile = new CompanyProfileInfo($company->profileInfoId);
				$msg->company = User::buildUserResponseFromObj($company);
				$msg->company->profile = CompanyProfileInfo::buildProfileResponseFromObj($profile);
			}
			
			$msg->title = $attr->title;
			$msg->text = $attr->text;
			$msg->amount = $attr->amount;
			$msg->count = $attr->count;
			
			$response[] = $msg;
		}
		
		return $response;
	}
}
